package com.zqxq.lottery.service.sys.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.config.YmlConfig;
import com.zqxq.lottery.entity.bo.sys.SysAppVersionBO;
import com.zqxq.lottery.entity.po.sys.DownloadCountPO;
import com.zqxq.lottery.entity.po.sys.SysAppVersionPO;
import com.zqxq.lottery.entity.vo.sys.SysAppVersionVO;
import com.zqxq.lottery.mapper.sys.DownloadCountMapper;
import com.zqxq.lottery.mapper.sys.SysAppVersionMapper;
import com.zqxq.lottery.service.sys.ISysAppVersionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class SysAppVersionServiceImpl extends ServiceImpl<SysAppVersionMapper,SysAppVersionPO> implements ISysAppVersionService {

    @Autowired
    private DownloadCountMapper downloadCountMapper;
    @Autowired
    private YmlConfig ymlConfig;
    @Override
    public SysAppVersionVO queryOne(SysAppVersionBO search) throws Exception{
        SysAppVersionVO app = new SysAppVersionVO();
        List<SysAppVersionVO> list = baseMapper.queryOne(search);
        if(null != list && list.size()>0){
            app = list.get(0);
            if(!app.getVersion().equals(search.getAppVersion()))
                app.setUpgrade(true);
        }
        return app;
    }

    @Override
    public Result saveChannelCount(String channel, String downSource) {
        if (StrUtils.isEmpty(channel) || StrUtils.isEmpty(downSource)){
            log.info("下载渠道统计失败channel或downSource为空");
            return new Result(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg());
        }
        DownloadCountPO downloadCountPO = new DownloadCountPO();
        downloadCountPO.setChannel(channel);
        downloadCountPO.setDown_source(Integer.parseInt(downSource));
        DownloadCountPO po = downloadCountMapper.selectOne(downloadCountPO);
        if (po == null){
            downloadCountPO = new DownloadCountPO();
            Long id = ymlConfig.getPkId();
            downloadCountPO.setId(id);
            if (StrUtils.isNotEmpty(downSource))
                downloadCountPO.setDown_source(Integer.parseInt(downSource));
            if (StrUtils.isNotEmpty(channel))
                downloadCountPO.setChannel(channel);
            downloadCountPO.setDown_num(1);
            downloadCountMapper.insert(downloadCountPO);
        }else {
            int down_num = po.getDown_num();
            po.setDown_num(down_num+1);
            downloadCountMapper.updateById(po);
        }
        return new Result(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg());
    }

}
